using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaDeploy
{
    //Table-Mapping Class (Customisable half)
    public partial class CPushedUpgrade
    {
		#region Constants
		private static string JOIN_INSTANCE = string.Concat(TABLE_NAME, " INNER JOIN ", CInstance.TABLE_NAME, " ON PushInstanceId=InstanceId"); //Nullable => Use LEFT OUTER JOIN
		#endregion

		#region Constructors (Public)
		//Default Connection String
		public CPushedUpgrade() : base() {}

        //Alternative Connection String
        public CPushedUpgrade(CDataSrc dataSrc) : base(dataSrc) {}
        
        //Hidden  (UI code should use cache instead)
        protected internal CPushedUpgrade(int pushId) : base(pushId) {}
        protected internal CPushedUpgrade(CDataSrc dataSrc, int pushId) : base(dataSrc, pushId) {}
        protected internal CPushedUpgrade(CDataSrc dataSrc, int pushId, IDbTransaction txOrNull) : base(dataSrc, pushId, txOrNull) { }
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            _pushStarted = DateTime.Now;
        }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
        #endregion

        #region Properties - Relationships
        //Relationships - Foriegn Keys (e.g parent)
        public CInstance Instance { get { return CInstance.Cache.GetById(this.PushInstanceId); } }
        public CVersion OldVersion { get { return CVersion.Cache.GetById(this.PushOldVersionId); } }
        public CVersion NewVersion { get { return CVersion.Cache.GetById(this.PushNewVersionId); } }
        public CBinaryFile OldSchema() { return new CBinaryFile(this.PushOldSchemaMD5); }
        public CBinaryFile NewSchema() { return new CBinaryFile(this.PushNewSchemaMD5); }


        //Relationships - Collections (e.g. children)
        #endregion

        #region Properties - Customisation
        //Derived/ReadOnly (e.g. xml classes, presentation logic)
        public string PushOldSchemaB64 { get { return CBinary.ToBase64(PushOldSchemaMD5, 6); } }
        public string PushNewSchemaB64 { get { return CBinary.ToBase64(PushNewSchemaMD5, 6); } }
		#endregion

		#region Save/Delete Overrides
		//Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
		#endregion

		#region Custom Database Queries
		//(Not normally required for cached classes, use list class for searching etc)
		//For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
		//For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)

		public int SelectCountByAppId(int appId) { return SelectCount(new CCriteriaList("InstanceAppId", appId), JOIN_INSTANCE); }
		#endregion

		#region Searching (Optional)
		//For cached classes, custom seach logic resides in static methods on the list class
		// e.g. CPushedUpgrade.Cache.Search("...")

		//See also the auto-generated methods based on indexes
		//' e.g. CPushedUpgrade.Cache.GetBy...
		#endregion

		#region Caching Details
		//Cache Key
		internal static string CACHE_KEY = typeof(CPushedUpgrade).ToString();    //TABLE_NAME

        //Cache data
        private static CPushedUpgradeList LoadCache()  {   return new CPushedUpgrade().SelectAll();   }
        //Cache Timeout
        private static void SetCache(CPushedUpgradeList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(CACHE_KEY, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CPushedUpgrade CacheGetById(CPushedUpgradeList list)  { return list.GetById(this.PushId);    }
        #endregion

        #region Cloning
        public CPushedUpgrade Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CPushedUpgrade copy = new CPushedUpgrade(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.PushId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}